package com.example.smartwaterapi.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信小程序配置类
 */
@Slf4j
@Configuration
@ConditionalOnClass(WxMaService.class)
public class WxMaConfiguration {

    /**
     * 微信小程序appId
     */
    @Value("${wechat.miniapp.app-id}")
    private String appId;

    /**
     * 微信小程序secret
     */
    @Value("${wechat.miniapp.secret}")
    private String secret;

    @Bean
    @ConditionalOnMissingBean(WxMaService.class)
    public WxMaService wxMaService() {
        log.info("初始化微信小程序服务，appId: {}", appId);
        
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(appId);
        config.setSecret(secret);
        
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        
        return service;
    }
} 